package oyl.selenium.config.translate;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.springframework.util.StringUtils;
import oyl.selenium.config.AbstractDriver;
import oyl.selenium.entity.Article;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Nicky.Tang on 2021/9/19 12:36 下午
 *
 * @since 02.12.10
 */
@Deprecated
public class TranslateUtilsOld {
    public static final String translateUrl = "https://translate.google.cn/";
    public static final Map<String, Map<String, String>> translateMap = new HashMap<>();
    public static String translateWindowHandle = null;

    public AbstractDriver abstractDriver;


    public TranslateUtilsOld(AbstractDriver abstractDriver) {
        this.abstractDriver = abstractDriver;
    }

    private void switchToTranslateTab() {
        if (TranslateUtilsOld.translateWindowHandle == null) {
            abstractDriver.open("首次打开谷歌翻译", By.className("ooArgc"),
                    () -> abstractDriver.executorDriver.executeScript("window.open('" + translateUrl + "')"));
            translateWindowHandle = abstractDriver.driver.getWindowHandle();
        } else {
            abstractDriver.switchTab(TranslateUtilsOld.translateWindowHandle);
        }
    }

    public void translate(List<Article> articleAll) {
        System.out.println("开始翻译。。。");
        GT trangGT = GT.getInstance();
        for (Article article : articleAll) {
            try {
                System.out.println("开始翻译: " + article.getLanguageCN());
                Map<String, String> res = new HashMap<>();
                res.put(article.getArticlebody(), trangGT.translateText(article.getArticlebody(), "en", article.getLanguage()));
                abstractDriver.sleep(500);
                res.put(article.getTitle(), trangGT.translateText(article.getTitle(), "en", article.getLanguage()));
                abstractDriver.sleep(500);
                res.put(article.getFixedurl(), trangGT.translateText(article.getFixedurl(), "en", article.getLanguage()));
                abstractDriver.sleep(500);
                res.put(article.getIntroduce(), trangGT.translateText(article.getIntroduce(), "en", article.getLanguage()));
                abstractDriver.sleep(500);
                TranslateUtilsOld.translateMap.put(article.getId(), res);
                TranslateUtils.translateMap.put(article.getId(), res);
            }catch (Exception ex){
                System.out.println("失败翻译: " + article.getLanguageCN());
                ex.printStackTrace();
            }
        }
        System.out.println("结束翻译。。。\n");
    }

    public void translate2(List<Article> articleAll) {
        switchToTranslateTab();

//        List<WebElement> moreLanguage = abstractDriver.driver.findElements(By.xpath(".//*[@jsname='zumM6d']"));
//        abstractDriver.executorDriver.executeScript("arguments[0].click();", moreLanguage.get(1));

        // List<WebElement> searchKeys = abstractDriver.driver.findElements(By.xpath(".//*[@jsname='oA4zhb']"));
        //abstractDriver.executorDriver.executeScript("arguments[0].click();",  searchKeys.get(1));

        List<WebElement> checkedLanguages = abstractDriver.driver.findElements(By.xpath(".//*[@jsname='Lm8Uhb']"));
//        List<WebElement> autoLanguages = abstractDriver.driver.findElements(By.className("VfPpkd-jY41G-V67aGc"));
//        WebElement autoLanguage = null;
//        for (int i = autoLanguages.size() - 1; i >= 0; i--) {
//            try {
//                WebElement autoLanguageItem = autoLanguages.get(i);
//                String innerText = autoLanguageItem.getAttribute("innerText");
//                if (innerText.equals("检测语言")) {
//                    autoLanguage = autoLanguageItem;
//                }
//            } catch (Exception ex) {
//                System.out.println("");
//            }
//        }
//        abstractDriver.executorDriver.executeScript("arguments[0].click();", autoLanguage);

//        abstractDriver.sleep(500);
        WebElement leftSource = abstractDriver.driver.findElement(By.xpath(".//*[@jsname='BJE2fc']"));
        leftSource.clear();
        leftSource.sendKeys("try use english");
        abstractDriver.sleep(500);

        WebElement leftEnglish = null;
        for (int i = 0; i < checkedLanguages.size(); i++) {
            try {
                WebElement checkedLanguage = checkedLanguages.get(i);
                String innerText = checkedLanguage.getAttribute("innerText");
                if (innerText.equals("英语")) {
                    leftEnglish = checkedLanguage;
                    break;
                }
            } catch (Exception ex) {
                System.out.println();
            }
        }
        for (Article article : articleAll) {
            System.out.println("开始翻译: " + article.getLanguageCN());
            for (int i = checkedLanguages.size() - 1; i >= 0; i--) {
                try {
                    WebElement checkedLanguage = checkedLanguages.get(i);
                    String innerText = checkedLanguage.getAttribute("innerText");
                    if (innerText.equals(article.getLanguageCN())) {
                        abstractDriver.executorDriver.executeScript("arguments[0].click();", checkedLanguage);
                    }
                } catch (Exception ex) {
                    System.out.println();
                }
            }
            abstractDriver.executorDriver.executeScript("arguments[0].click();", leftEnglish);
            abstractDriver.sleep(500);
            Map<String, String> res = new HashMap<>();
            doTranslateStr(res, leftSource, article.getArticlebody());
            doTranslateStr(res, leftSource, article.getTitle());
            doTranslateStr(res, leftSource, article.getFixedurl());
            TranslateUtilsOld.translateMap.put(article.getId(), res);
        }

//        abstractDriver.switchToLastTab();
    }


    private void doTranslateStr(Map<String, String> res, WebElement leftSource, String source) {

        abstractDriver.executorDriver.executeScript("arguments[0].value='';", leftSource);
//        leftSource.clear();
//        abstractDriver.executorDriver.executeScript("arguments[0].value='';", rightTarget);


        leftSource.sendKeys(source);
        abstractDriver.sleep(2500);

        boolean resSuccess = abstractDriver.tryDo(20, () -> {
            WebElement rightTarget = abstractDriver.driver.findElement(By.xpath(".//*[@jsname='YPqjbf']"));
            String target = rightTarget.getAttribute("innerText");
            if (!StringUtils.isEmpty(target)) {
                res.put(source, target);
                return true;
            }
            return false;
        });

        if (!resSuccess) {
            System.out.println(source + " 翻译失败，用回原来的。。。");
            res.put(source, source);
        }
    }

}
